package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.search.query.StrategySearchQuery;
import cn.wolfcode.luowowo.search.service.*;
import cn.wolfcode.luowowo.search.template.DestinationTemplate;
import cn.wolfcode.luowowo.search.template.StrategyTemplate;
import cn.wolfcode.luowowo.search.template.TravelTemplate;
import cn.wolfcode.luowowo.search.template.UserInfoTemplate;
import cn.wolfcode.luowowo.search.util.SearchResult;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class SearchController {

    @Reference
    private IStrategySearchService strategySearchService;

    @Reference
    private IDestinationSearchService destinationSearchService;

    @Reference
    private ITravelSearchService travelSearchService;

    @Reference
    private IUserInfoSearchService userInfoSearchService;

    @Reference
    ISearchService searchService;


    @RequestMapping("q")
    private String q(Model m, @ModelAttribute("qo") StrategySearchQuery qo) {
        //type :  -1  全部
        //type :  0  目的地
        //type :  1  攻略
        //type :  2  游记
        //type :  3  用户

        switch (qo.getType()) {

            case 0:   //目的地
                return destSearch(m, qo);
            case 1:   //攻略
                return strategySearch(m, qo);
            case 2:   //游记
                return travelSearch(m, qo);
            case 3:   //用户
                return userInfoSearch(m, qo);
            default:   //全部
                return allSearch(m, qo);
        }
    }

    //用户搜索
    private String userInfoSearch(Model m, StrategySearchQuery qo) {
        //用户
        Page<UserInfoTemplate> users = searchService.searchWithHighlight(UserInfoTemplate.INDEX_NAME, UserInfoTemplate.TYPE_NAME,
                UserInfoTemplate.class, qo, "nickName", "destName");
        m.addAttribute("page",users);
        return "index/searchUser";
    }

    //查询所有
    private String allSearch(Model m, StrategySearchQuery qo) {
        //目的地
        Page<DestinationTemplate> dests = searchService.searchWithHighlight(DestinationTemplate.INDEX_NAME, DestinationTemplate.TYPE_NAME,
                DestinationTemplate.class, qo, "name", "info");

        //攻略
        Page<StrategyTemplate> strategys = searchService.searchWithHighlight(StrategyTemplate.INDEX_NAME, StrategyTemplate.TYPE_NAME,
                StrategyTemplate.class, qo, "title", "subTitle", "summary");

        //游记
        Page<TravelTemplate> travels = searchService.searchWithHighlight(TravelTemplate.INDEX_NAME, TravelTemplate.TYPE_NAME,
                TravelTemplate.class, qo, "title", "summary");

        //用户
        Page<UserInfoTemplate> users = searchService.searchWithHighlight(UserInfoTemplate.INDEX_NAME, UserInfoTemplate.TYPE_NAME,
                UserInfoTemplate.class, qo, "nickName", "destName");
        SearchResult ret = new SearchResult();
        ret.setUsers(users.getContent());
        ret.setStrategys(strategys.getContent());
        ret.setDests(dests.getContent());
        ret.setTravels(travels.getContent());
        Long count = strategys.getTotalElements() + travels.getTotalElements() + users.getTotalElements()+dests.getTotalElements();
        ret.setTotal(count);
        m.addAttribute("data",ret);
        return "index/searchAll";
    }

    //游记搜索
    private String travelSearch(Model m, StrategySearchQuery qo) {
        //游记
        Page<TravelTemplate> travels = searchService.searchWithHighlight(TravelTemplate.INDEX_NAME, TravelTemplate.TYPE_NAME,
                TravelTemplate.class, qo, "title", "summary");
        m.addAttribute("page",travels);
        return "index/searchTravel";
    }

    //攻略搜索
    private String strategySearch(Model m, StrategySearchQuery qo) {
        //攻略
        Page<StrategyTemplate> strategys = searchService.searchWithHighlight(StrategyTemplate.INDEX_NAME, StrategyTemplate.TYPE_NAME,
                StrategyTemplate.class, qo, "title", "subTitle", "summary");
        m.addAttribute("page",strategys);
        return "index/searchStrategy";
    }

    //目的地搜索
    private String destSearch(Model m, StrategySearchQuery qo) {

        //1.根据关键字查询目的地对象
        SearchResult ret = new SearchResult();
        DestinationTemplate dest = destinationSearchService.queryDestByName(qo.getKeyword());
        //如果存在,查询该目的地下所有的攻略,游记,用户
        if (dest != null) {
            qo.setDestId(dest.getId());
            qo.setPageSize(5);
            Page<TravelTemplate> travels = travelSearchService.query(qo);

            Page<StrategyTemplate> strategys = strategySearchService.queryByDestId(qo);

            Page<UserInfoTemplate> users = userInfoSearchService.queryUserByDestName(qo);
            ret.setStrategys(strategys.getContent());
            ret.setTravels(travels.getContent());
            ret.setUsers(users.getContent());
            Long count = strategys.getTotalElements() + travels.getTotalElements() + users.getTotalElements();
            ret.setTotal(count);
            m.addAttribute("data", ret);
        }
        //如果不存在,查询条数为0
        //qo

        //data

        //dest
        m.addAttribute("dest", dest);

        return "index/searchDest";
    }
}
